package jmine.tec.web.front.services.incluir;

import java.util.Set;

import jmine.tec.database.pack.PackException;
import jmine.tec.database.pack.services.PackEntityPersistenceHelper;
import jmine.tec.di.annotation.Injected;
import jmine.tec.services.annotations.Execution;
import jmine.tec.services.annotations.Input;
import jmine.tec.services.annotations.ServiceImplementor;
import jmine.tec.services.annotations.WithPreffix;
import jmine.tec.services.api.ActionsEnum;
import jmine.tec.services.api.RawInput;
import jmine.tec.services.api.export.ReferenceMap;
import jmine.tec.services.api.export.ServiceFiller;
import jmine.tec.services.export.impl.ExportUtils;
import jmine.tec.services.io.ServiceBean;
import jmine.tec.web.front.domain.BackOfficeRequestProperty;
import jmine.tec.web.front.domain.BackOfficeRequestType;

/**
 * Service for creating {@link BackOfficeRequestType}
 * 
 * @author vinicius
 */
@ServiceImplementor(action = ActionsEnum.INCLUIR)
public class BackOfficeRequestTypeService implements ServiceFiller<BackOfficeRequestType> {

    public static final String MNEMONICO = "Mnemônico";

    public static final String METADATA = "Metadata";

    public static final String PROPERTY_PREFFIX = "Property ";

    public static final String DATABASE_ID = "Database ID";

    private Long databaseId;

    private String mnemonic;

    private RawInput metadata;

    private Set<BackOfficeRequestProperty> properties;

    private PackEntityPersistenceHelper helper;

    /**
     * @param helper the helper to set
     */
    @Injected
    public void setHelper(PackEntityPersistenceHelper helper) {
        this.helper = helper;
    }

    /**
     * @throws PackException exceção na hora de salvar o mnemonico ou o formHandler.
     */
    @Execution
    public void execute() throws PackException {
        BackOfficeRequestType type = this.helper.findOrCreate(BackOfficeRequestType.class, this.databaseId, this.mnemonic);
        type.setMnemonic(this.mnemonic);
        type.setMetadata(this.metadata.getRaw(METADATA));
        type.clearProperties();
        type.addAllProperties(this.properties);
        this.helper.save(type, this.databaseId);
    }

    /**
     * @param databaseId the databaseId to set
     */
    @Input(fieldName = DATABASE_ID, required = true)
    public void setDatabaseId(Long databaseId) {
        this.databaseId = databaseId;
    }

    /**
     * @param mnemonic the mnemonic to set
     */
    @Input(fieldName = MNEMONICO, required = true)
    public void setMnemonic(String mnemonic) {
        this.mnemonic = mnemonic;
    }

    /**
     * @param properties the properties to set
     */
    @WithPreffix(preffix = PROPERTY_PREFFIX)
    public void setProperties(Set<BackOfficeRequestProperty> properties) {
        this.properties = properties;
    }

    /**
     * @param metadata the type metadata bean to set
     */
    @Injected
    public void setMetadata(RawInput metadata) {
        this.metadata = metadata;
    }

    /**
     * @return the metadata bean identifier
     */
    public RawInput getMetadata() {
        return this.metadata;
    }

    /**
     * {@inheritDoc}
     */
    public void fillServiceBean(ServiceBean bean, ReferenceMap referenceMap, BackOfficeRequestType entity) {
        ExportUtils.setService(bean, this.getClass());
        ExportUtils.putDatabaseId(bean, DATABASE_ID, entity);
        ExportUtils.put(bean, MNEMONICO, entity.getMnemonic());
        ExportUtils.put(bean, METADATA, entity.getMetadata());
        for (BackOfficeRequestProperty property : entity.properties()) {
            ExportUtils.put(bean, PROPERTY_PREFFIX + entity.properties().indexOf(property), property.getMnemonic());
        }
    }

}
